﻿package com.tudou.home.model.ad
{
    import com.tudou.ad.events.*;
    import com.tudou.ad.view.*;
    import com.tudou.home.commands.ad.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.model.ad.base.*;
    import com.tudou.home.view.*;
    import flash.display.*;
    import flash.geom.*;

    public class AdPauseProxy extends AdVideoProxyBase
    {
        public static const NAME:String = "AdPauseProxy";

        public function AdPauseProxy()
        {
            super(NAME);
            loaderTime = 0;
            return;
        }// end function

        override protected function loadFront() : void
        {
            initFront();
            trace("loadFront : " + adItemInfo.url);
            startLoadFront(adItemInfo.url);
            return;
        }// end function

        override protected function frontStuffComplete() : void
        {
            if (adItemInfo && !adItemInfo.isVideo)
            {
                prepareToShow();
            }
            if (view)
            {
                view.setStuff(frontStuff);
                view.addToShowStuff();
            }
            return;
        }// end function

        override public function startView() : void
        {
            if (!startViewCommon())
            {
                return;
            }
            view = new AdPauseView();
            view.mainWidth = AdSize.PAUSED_WIDTH;
            view.mainHeight = AdSize.PAUSED_HEIGHT;
            view.bgWidth = AdSize.PAUSED_WIDTH;
            view.bgHeight = AdSize.PAUSED_HEIGHT;
            view.mainTop = PlayerConsts.VIDEO_TOP;
            view.adItemInfo = adItemInfo;
            view.adThrowInfo = adThrowInfo;
            view.countDownTime = getRemainDt();
            view.isExpand = PlayerConsts.expandStage;
            view.tracker = _tracker;
            view.initToShow();
            createViewListeners();
            var _loc_1:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            var _loc_2:* = _loc_1.isFullScreen ? (PlayerConsts.FULLSCREEN_RECT) : (PlayerConsts.NORMALSCREEN_RECT);
            goTransform(_loc_2.width, _loc_2.height, _loc_1.isFullScreen);
            view.startAd();
            return;
        }// end function

        override protected function get frontStuff() : DisplayObject
        {
            return loader ? (loader.content) : (null);
        }// end function

        private function prepareEndShow() : void
        {
            if (adItemInfo.isVideo)
            {
                trace("prepareEndShow :ok");
                loadFront();
            }
            return;
        }// end function

        override protected function adV2End() : void
        {
            loadedV2 = true;
            prepareToShow();
            return;
        }// end function

        override protected function adErrorEnd() : void
        {
            sendNotification(AdPauseCommandSet.ERROR_END);
            return;
        }// end function

        override protected function viewEventHlr(event:AdItemViewEvent) : void
        {
            trace("viewEventHlr:" + event.type);
            switch(event.type)
            {
                case AdItemViewEvent.SHOW_START:
                {
                    sendNotification(AdPauseCommandSet.SHOW_START);
                    break;
                }
                case AdItemViewEvent.SHOW_NEAR_END:
                {
                    prepareEndShow();
                    break;
                }
                case AdItemViewEvent.SHOW_END:
                {
                    sendNotification(AdPauseCommandSet.SHOW_END);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override protected function delayTimerComplete() : void
        {
            if (adItemInfo.isVideo)
            {
                loadV2();
            }
            else
            {
                loadFront();
            }
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            if (!view)
            {
                return;
            }
            view.goTransform(param1, param2, param3);
            return;
        }// end function

        override protected function frontStuffError() : void
        {
            if (!adItemInfo.isVideo)
            {
                adSkip();
            }
            adErrorEnd();
            return;
        }// end function

        private function prepareToShow() : void
        {
            startView();
            sendNotification(AdPauseCommandSet.PREPARE_TO_SHOW);
            return;
        }// end function

    }
}
